//=============================================================================
// DirectInput.cpp by Frank Luna (C) 2005 All Rights Reserved.
//=============================================================================
#include "DirectInput.h"

DirectInput* gDInput = 0;

DirectInput::DirectInput(DWORD keyboardCoopFlags, DWORD mouseCoopFlags, HINSTANCE mHInstance, HWND mHWnd)
{
	ZeroMemory(mKeyboardState, sizeof(mKeyboardState));
	ZeroMemory(&mMouseState, sizeof(mMouseState));

	DirectInput8Create(mHInstance,
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		( void** )&mDInput,
		0 ) ;

	mDInput->CreateDevice( GUID_SysKeyboard, &mKeyboard, 0 ) ;
	mKeyboard->SetDataFormat( &c_dfDIKeyboard ) ;
	mKeyboard->SetCooperativeLevel( mHWnd, keyboardCoopFlags ) ;
	mKeyboard->Acquire( ) ;

	mDInput->CreateDevice( GUID_SysMouse, &mMouse, 0 ) ;
	mMouse->SetDataFormat( &c_dfDIMouse2 ) ;
	mMouse->SetCooperativeLevel( mHWnd, mouseCoopFlags ) ;
	mMouse->Acquire( ) ;
}

DirectInput::~DirectInput()
{
	mDInput->Release();
	mKeyboard->Unacquire();
	mMouse->Unacquire();
	mKeyboard->Release();
	mMouse->Release();
}

void DirectInput::poll()
{
	// Poll keyboard.
	HRESULT hr = mKeyboard->GetDeviceState(sizeof(mKeyboardState), (void**)&mKeyboardState); 
	if( FAILED(hr) )
	{
		// Keyboard lost, zero out keyboard data structure.
		ZeroMemory(mKeyboardState, sizeof(mKeyboardState));

		 // Try to acquire for next time we poll.
		hr = mKeyboard->Acquire();
	}

	// Poll mouse.
	hr = mMouse->GetDeviceState(sizeof(DIMOUSESTATE2), (void**)&mMouseState); 
	if( FAILED(hr) )
	{
		// Mouse lost, zero out mouse data structure.
		ZeroMemory(&mMouseState, sizeof(mMouseState));

		// Try to acquire for next time we poll.
		hr = mMouse->Acquire(); 
	}
}

//returns true if certain key is pressed
bool DirectInput::keyDown(int key)
{
	return (mKeyboardState[key] & 0x80) != 0;
}

//return when mouse button is pressed
bool DirectInput::mouseButtonDown(int button)
{
	return (mMouseState.rgbButtons[button] & 0x80) != 0;
}

float DirectInput::mouseDX()
{
	return (float)mMouseState.lX;
}

float DirectInput::mouseDY()
{
	return (float)mMouseState.lY;
}

float DirectInput::mouseDZ()
{
	return (float)mMouseState.lZ;
}